{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a9f54d3e-798e-46c2-8fb9-4ebeab865f55",
   "metadata": {},
   "source": [
    "# 分类模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60720a62-219e-4b0a-bea8-476bdaf781b5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f17154e9-50ee-4404-94d2-7fb4239dbc11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基准测试通过\n",
    "# 来源：https://github.com/sipeed/maix_train"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d35ee878-3599-42ec-bc3c-2307dc4103b2",
   "metadata": {},
   "source": [
    "## 数据配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d3dfbdc6-9a6a-4b76-b189-eba5266c1255",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把你的 xx.zip 数据上到 work目录，【注意，解压得到的文件名称】参考例子数据\n",
    "upload_zip = f\"../datasets/classifier_datasets.zip\"# demo数据\n",
    "data_unzip = f\"../datasets/ts_classifier\"\n",
    "data_unzip_last = \"../datasets/ts_test_classifier_datasets\" #最后解压后的目录(直接本地解压好了)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c32c5b63-15f2-46d5-8895-5d3679344aef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "cmd = f\"unzip -o -d {data_unzip} {upload_zip}\"\n",
    "# f=os.popen(cmd)\n",
    "# print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55bc3a0d-8f35-43a3-9b15-161e0b6d531d",
   "metadata": {},
   "source": [
    "## python包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "14b91a0a-7d8d-45fb-9925-8359cced3e8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "09a17c45-2d27-4a7f-b735-6140cc6b0e0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from train.classifier_v1 import classifier_train\n",
    "from train.classifier.instance import config\n",
    "\n",
    "config.classifier_train_epochs = 1 # 修改训练轮数 40"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3719f0d-b37a-4be2-98cf-c650afad18ff",
   "metadata": {},
   "source": [
    "## config setting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "05715507-1f9a-4b67-9fd4-25686950f8c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-09-27_09_53\n"
     ]
    }
   ],
   "source": [
    "cur_time = time.strftime(\"%Y-%m-%d_%H_%M\", time.localtime())\n",
    "print(cur_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "19b14c7e-a890-423e-ad0d-f8de457c7ead",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "root_dir: /Users/liampro/Downloads/pro/git/ai-test/k210\n",
      "temp_saves_dir: /Users/liampro/Downloads/pro/git/ai-test/k210/ubuntu/out/imgn_2021-09-27_09_53/\n",
      "datasets_dir: /Users/liampro/Downloads/pro/git/ai-test/k210/datasets/ts_test_classifier_datasets\n"
     ]
    }
   ],
   "source": [
    "root_dir =  os.path.abspath('../')\n",
    "ubuntu_path = f\"out/imgn_{cur_time}\" #用来mac中生成kmodel\n",
    "temp_saves_dir = os.path.join(root_dir,f\"ubuntu/{ubuntu_path}/\") #保存文件目录\n",
    "datasets_dir = os.path.join(root_dir,\"datasets/ts_test_classifier_datasets\") #数据源\n",
    "datasets_dir_zip = data_unzip_last #os.path.join(root_dir,\"datasets/classifier_datasets.zip\") #数据源 zip (images.zip)\n",
    "\n",
    "print(\"root_dir:\",root_dir)\n",
    "print(\"temp_saves_dir:\",temp_saves_dir)\n",
    "print(\"datasets_dir:\",datasets_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c08d2070-1af8-4f41-b4b1-1d05614bfba0",
   "metadata": {},
   "source": [
    "## 训练函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "181fc08f-9485-4b38-8a46-d33258914e44",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 清理隐藏文件\n",
    "cmd1 = f\"rm -f {datasets_dir}/.DS_Store\"\n",
    "os.popen(cmd1)\n",
    "cmd = f\"rm -f {datasets_dir}/*/.DS_Store\"\n",
    "f = os.popen(cmd)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b01038f-6177-4415-a431-fc3928f66ff8",
   "metadata": {},
   "source": [
    "## 训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a8ac7a7e-1ad1-49a8-97f0-702a21b5fd49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "select gpu fail:NVML Shared Library Not Found\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-26 17:09:36,628 - [INFO]:  no GPU, will use [CPU]\n",
      "2021-09-26 17:09:40,608 - [INFO]:  train, labels:['0', '1']\n",
      "2021-09-26 17:09:40,609 - [DEBUG]:  train, datasets dir:../datasets/ts_test_classifier_datasets\n",
      "2021-09-26 17:09:40.686158: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "/opt/anaconda3/envs/k210v37/lib/python3.7/site-packages/keras/optimizer_v2/optimizer_v2.py:356: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.\n",
      "  \"The `lr` argument is deprecated, use `learning_rate` instead.\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "input_1 (InputLayer)         [(None, 224, 224, 3)]     0         \n",
      "_________________________________________________________________\n",
      "conv1_pad (ZeroPadding2D)    (None, 226, 226, 3)       0         \n",
      "_________________________________________________________________\n",
      "conv1 (Conv2D)               (None, 112, 112, 24)      648       \n",
      "_________________________________________________________________\n",
      "conv1_bn (BatchNormalization (None, 112, 112, 24)      96        \n",
      "_________________________________________________________________\n",
      "conv1_relu (ReLU)            (None, 112, 112, 24)      0         \n",
      "_________________________________________________________________\n",
      "conv_dw_1 (DepthwiseConv2D)  (None, 112, 112, 24)      216       \n",
      "_________________________________________________________________\n",
      "conv_dw_1_bn (BatchNormaliza (None, 112, 112, 24)      96        \n",
      "_________________________________________________________________\n",
      "conv_dw_1_relu (ReLU)        (None, 112, 112, 24)      0         \n",
      "_________________________________________________________________\n",
      "conv_pw_1 (Conv2D)           (None, 112, 112, 48)      1152      \n",
      "_________________________________________________________________\n",
      "conv_pw_1_bn (BatchNormaliza (None, 112, 112, 48)      192       \n",
      "_________________________________________________________________\n",
      "conv_pw_1_relu (ReLU)        (None, 112, 112, 48)      0         \n",
      "_________________________________________________________________\n",
      "conv_pad_2 (ZeroPadding2D)   (None, 114, 114, 48)      0         \n",
      "_________________________________________________________________\n",
      "conv_dw_2 (DepthwiseConv2D)  (None, 56, 56, 48)        432       \n",
      "_________________________________________________________________\n",
      "conv_dw_2_bn (BatchNormaliza (None, 56, 56, 48)        192       \n",
      "_________________________________________________________________\n",
      "conv_dw_2_relu (ReLU)        (None, 56, 56, 48)        0         \n",
      "_________________________________________________________________\n",
      "conv_pw_2 (Conv2D)           (None, 56, 56, 96)        4608      \n",
      "_________________________________________________________________\n",
      "conv_pw_2_bn (BatchNormaliza (None, 56, 56, 96)        384       \n",
      "_________________________________________________________________\n",
      "conv_pw_2_relu (ReLU)        (None, 56, 56, 96)        0         \n",
      "_________________________________________________________________\n",
      "conv_dw_3 (DepthwiseConv2D)  (None, 56, 56, 96)        864       \n",
      "_________________________________________________________________\n",
      "conv_dw_3_bn (BatchNormaliza (None, 56, 56, 96)        384       \n",
      "_________________________________________________________________\n",
      "conv_dw_3_relu (ReLU)        (None, 56, 56, 96)        0         \n",
      "_________________________________________________________________\n",
      "conv_pw_3 (Conv2D)           (None, 56, 56, 96)        9216      \n",
      "_________________________________________________________________\n",
      "conv_pw_3_bn (BatchNormaliza (None, 56, 56, 96)        384       \n",
      "_________________________________________________________________\n",
      "conv_pw_3_relu (ReLU)        (None, 56, 56, 96)        0         \n",
      "_________________________________________________________________\n",
      "conv_pad_4 (ZeroPadding2D)   (None, 58, 58, 96)        0         \n",
      "_________________________________________________________________\n",
      "conv_dw_4 (DepthwiseConv2D)  (None, 28, 28, 96)        864       \n",
      "_________________________________________________________________\n",
      "conv_dw_4_bn (BatchNormaliza (None, 28, 28, 96)        384       \n",
      "_________________________________________________________________\n",
      "conv_dw_4_relu (ReLU)        (None, 28, 28, 96)        0         \n",
      "_________________________________________________________________\n",
      "conv_pw_4 (Conv2D)           (None, 28, 28, 192)       18432     \n",
      "_________________________________________________________________\n",
      "conv_pw_4_bn (BatchNormaliza (None, 28, 28, 192)       768       \n",
      "_________________________________________________________________\n",
      "conv_pw_4_relu (ReLU)        (None, 28, 28, 192)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_5 (DepthwiseConv2D)  (None, 28, 28, 192)       1728      \n",
      "_________________________________________________________________\n",
      "conv_dw_5_bn (BatchNormaliza (None, 28, 28, 192)       768       \n",
      "_________________________________________________________________\n",
      "conv_dw_5_relu (ReLU)        (None, 28, 28, 192)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_5 (Conv2D)           (None, 28, 28, 192)       36864     \n",
      "_________________________________________________________________\n",
      "conv_pw_5_bn (BatchNormaliza (None, 28, 28, 192)       768       \n",
      "_________________________________________________________________\n",
      "conv_pw_5_relu (ReLU)        (None, 28, 28, 192)       0         \n",
      "_________________________________________________________________\n",
      "conv_pad_6 (ZeroPadding2D)   (None, 30, 30, 192)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_6 (DepthwiseConv2D)  (None, 14, 14, 192)       1728      \n",
      "_________________________________________________________________\n",
      "conv_dw_6_bn (BatchNormaliza (None, 14, 14, 192)       768       \n",
      "_________________________________________________________________\n",
      "conv_dw_6_relu (ReLU)        (None, 14, 14, 192)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_6 (Conv2D)           (None, 14, 14, 384)       73728     \n",
      "_________________________________________________________________\n",
      "conv_pw_6_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_6_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_7 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_7_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_7_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_7 (Conv2D)           (None, 14, 14, 384)       147456    \n",
      "_________________________________________________________________\n",
      "conv_pw_7_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_7_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_8 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_8_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_8_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_8 (Conv2D)           (None, 14, 14, 384)       147456    \n",
      "_________________________________________________________________\n",
      "conv_pw_8_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_8_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_9 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_9_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_9_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_9 (Conv2D)           (None, 14, 14, 384)       147456    \n",
      "_________________________________________________________________\n",
      "conv_pw_9_bn (BatchNormaliza (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_9_relu (ReLU)        (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_10_bn (BatchNormaliz (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_10_relu (ReLU)       (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_10 (Conv2D)          (None, 14, 14, 384)       147456    \n",
      "_________________________________________________________________\n",
      "conv_pw_10_bn (BatchNormaliz (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_10_relu (ReLU)       (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_11_bn (BatchNormaliz (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_11_relu (ReLU)       (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_11 (Conv2D)          (None, 14, 14, 384)       147456    \n",
      "_________________________________________________________________\n",
      "conv_pw_11_bn (BatchNormaliz (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_pw_11_relu (ReLU)       (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_12 (DepthwiseConv2D) (None, 14, 14, 384)       3456      \n",
      "_________________________________________________________________\n",
      "conv_dw_12_bn (BatchNormaliz (None, 14, 14, 384)       1536      \n",
      "_________________________________________________________________\n",
      "conv_dw_12_relu (ReLU)       (None, 14, 14, 384)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_12 (Conv2D)          (None, 14, 14, 768)       294912    \n",
      "_________________________________________________________________\n",
      "conv_pw_12_bn (BatchNormaliz (None, 14, 14, 768)       3072      \n",
      "_________________________________________________________________\n",
      "conv_pw_12_relu (ReLU)       (None, 14, 14, 768)       0         \n",
      "_________________________________________________________________\n",
      "conv_dw_13 (DepthwiseConv2D) (None, 14, 14, 768)       6912      \n",
      "_________________________________________________________________\n",
      "conv_dw_13_bn (BatchNormaliz (None, 14, 14, 768)       3072      \n",
      "_________________________________________________________________\n",
      "conv_dw_13_relu (ReLU)       (None, 14, 14, 768)       0         \n",
      "_________________________________________________________________\n",
      "conv_pw_13 (Conv2D)          (None, 14, 14, 768)       589824    \n",
      "_________________________________________________________________\n",
      "conv_pw_13_bn (BatchNormaliz (None, 14, 14, 768)       3072      \n",
      "_________________________________________________________________\n",
      "conv_pw_13_relu (ReLU)       (None, 14, 14, 768)       0         \n",
      "_________________________________________________________________\n",
      "global_average_pooling2d (Gl (None, 768)               0         \n",
      "_________________________________________________________________\n",
      "dropout (Dropout)            (None, 768)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 2)                 1538      \n",
      "=================================================================\n",
      "Total params: 1,834,514\n",
      "Trainable params: 1,538\n",
      "Non-trainable params: 1,832,976\n",
      "_________________________________________________________________\n",
      "Found 480 images belonging to 2 classes.\n",
      "self.labels:::: dict_keys(['0', '1'])\n",
      "Found 120 images belonging to 2 classes.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-26 17:09:41,351 - [INFO]:  train data:480, valid data:120\n",
      "/opt/anaconda3/envs/k210v37/lib/python3.7/site-packages/keras/engine/training.py:1972: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.\n",
      "  warnings.warn('`Model.fit_generator` is deprecated and '\n",
      "2021-09-26 17:09:41,459 - [INFO]:  train start\n",
      "2021-09-26 17:09:41,460 - [INFO]:  progress: 1.0%, train start\n",
      "2021-09-26 17:09:41.471402: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)\n",
      "2021-09-26 17:09:41,498 - [INFO]:  epoch 0 start\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "96/96 [==============================] - 12s 110ms/step - loss: 0.5633 - accuracy: 0.7396 - val_loss: 0.4859 - val_accuracy: 0.8000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-26 17:09:53,256 - [INFO]:  epoch 0 end: {'loss': 0.5632925033569336, 'accuracy': 0.7395833134651184, 'val_loss': 0.4859217405319214, 'val_accuracy': 0.800000011920929}\n",
      "2021-09-26 17:09:53,257 - [INFO]:  progress: 98.0%, train epoch end\n",
      "2021-09-26 17:09:53,258 - [INFO]:  train end\n",
      "2021-09-26 17:09:53,259 - [INFO]:  progress: 98.0%, train end\n",
      "2021-09-26 17:09:53,306 - [INFO]:  save model as .h5 file\n",
      "2021-09-26 17:09:53,489 - [INFO]:  save model as .tflite file\n",
      "2021-09-26 17:09:57.528548: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Assets written to: /var/folders/_l/dffcyljj723fg4wbqfxfqv640000gn/T/tmpkj1daqxs/assets\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-26 17:10:04.364201: I tensorflow/core/grappler/devices.cc:75] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA or ROCm support)\n",
      "2021-09-26 17:10:04.364288: I tensorflow/core/grappler/clusters/single_machine.cc:357] Starting new session\n",
      "2021-09-26 17:10:04.370952: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:1137] Optimization results for grappler item: graph_to_optimize\n",
      "  function_optimizer: function_optimizer did nothing. time = 0.005ms.\n",
      "  function_optimizer: function_optimizer did nothing. time = 0.001ms.\n",
      "\n",
      "2021-09-26 17:10:05.152072: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:351] Ignored output_format.\n",
      "2021-09-26 17:10:05.152092: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:354] Ignored drop_control_dependency.\n",
      "2021-09-26 17:10:05.186897: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:210] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.\n",
      "2021-09-26 17:10:05,282 - [INFO]:  train ok, now generate report\n",
      "INFO:logger: train ok, now generate report\n",
      "2021-09-26 17:10:05,284 - [INFO]:  generate report image\n",
      "INFO:logger: generate report image\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 600 images belonging to 2 classes.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/anaconda3/envs/k210v37/lib/python3.7/site-packages/keras/engine/training.py:2035: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.\n",
      "  warnings.warn('`Model.predict_generator` is deprecated and '\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120/120 [==============================] - 8s 60ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-26 17:10:13,834 - [INFO]:  generate report image end\n",
      "INFO:logger: generate report image end\n"
     ]
    }
   ],
   "source": [
    "# 配置文件：train.classifier.instance\n",
    "result,_,model_path ,labels = classifier_train(datasets_dir=datasets_dir,temp_datasets_dir=temp_saves_dir,config=config)\n",
    "\n",
    "#result,_,model_path ,labels = classifier_train(datasets_zip_path=datasets_dir_zip,temp_datasets_dir=temp_saves_dir,config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7a374535-3e6b-45fc-a1e9-f78d5650b4cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../ubuntu/out/imgn_2021-09-26_17_09/m.h5\n",
      "labels: dict_keys(['0', '1'])\n"
     ]
    }
   ],
   "source": [
    "print(model_path)\n",
    "print(\"labels:\",labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0099b74a-8dc0-438f-ba6e-3c6356315865",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5ed81877-bcad-4622-8819-9dff0ec637f2",
   "metadata": {},
   "source": [
    "## 生成kmodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "40a861b5-712c-40ed-90cf-0d9b3a4a66c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "docker run -it -v /Users/liampro/Downloads/pro/git/ai-test/k210/ubuntu:/train ubuntu /train/tokmodel.sh out/imgn_2021-09-26_17_09\n"
     ]
    }
   ],
   "source": [
    "#直接生成kmodel:Mac开发环境要弄个docker跑linux命令，\n",
    "# win,linux可以直接跑官方命令\n",
    "# tool:https://github.com/kendryte/nncase/releases/tag/v0.1.0-rc5\n",
    "\n",
    "exe_path = os.path.join(root_dir, \"ubuntu\")\n",
    "\n",
    "docker_cmd=f\"docker run -it -v {exe_path}:/train ubuntu /train/tokmodel.sh {ubuntu_path}\"\n",
    "print(docker_cmd)\n",
    "\n",
    "# 在shell 运行 docker命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "08059d53-0443-47a9-8817-70dac550fe5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../ubuntu/ncc-linux-x86_64/ncc -i tflite -o k210model --dataset ../ubuntu/out/imgn_2021-09-26_17_09/sample_images ../ubuntu/out/imgn_2021-09-26_17_09/m.tflite ../ubuntu/out/imgn_2021-09-26_17_09/result/m.kmodel\n"
     ]
    }
   ],
   "source": [
    "# linux\n",
    "import os\n",
    "\n",
    "ncc_path = \"../ubuntu/ncc-linux-x86_64/ncc\" # \n",
    "cmd_linux = f\"{ncc_path} -i tflite -o k210model --dataset {temp_saves_dir}sample_images {temp_saves_dir}m.tflite {temp_saves_dir}result/m.kmodel\"\n",
    "print(cmd_linux)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "36537249-441c-484e-a058-b44f5852ebcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f=os.popen(cmd_linux)\n",
    "# print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2578ccfb-878b-4cba-9160-1740cae98feb",
   "metadata": {},
   "source": [
    "## 生成k210程序boot.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5485ec54-29c7-4393-b317-a07bd737425b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cp -f ..//k210_train/data/template/imgn_boot.py ../ubuntu/out/imgn_2021-09-26_17_09/result/boot.py\n"
     ]
    }
   ],
   "source": [
    "target_file = f\"{temp_saves_dir}result/boot.py\"\n",
    "copy_file = f\"cp -f {root_dir}/k210_train/data/template/imgn_boot.py {target_file}\"\n",
    "print(copy_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e72b835-a5e8-4a31-95d5-cb93b1131cbb",
   "metadata": {},
   "source": [
    "## 生成k210程序boot.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7415aabb-dccd-429c-8629-7c1ec1fdbf47",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "24c899b9-3132-470c-ad57-a109a8ced60e",
   "metadata": {},
   "source": [
    "## 更新到k210 的cube硬件上运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "62752c83-5de8-48db-9514-af03999b05ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "f=os.popen(copy_file)\n",
    "print(f.read()) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "11688be9-976a-46e5-a8d4-5059456ff4cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n把 \\n├── result\\n│\\xa0\\xa0 ├── boot.py\\n│\\xa0\\xa0 └── m.kmodel\\n\\n的 boot.py ，m.kmodel 复制到 sd 卡的根目录下（或者 models/m8/目录下）\\n重新启动硬件，就可以运行了\\nboot.py 代码中核实模型的路径是否正确\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "把 \n",
    "├── result\n",
    "│   ├── boot.py\n",
    "│   └── m.kmodel\n",
    "\n",
    "的 boot.py ，m.kmodel 复制到 sd 卡的根目录下（或者 models/m8/目录下）\n",
    "重新启动硬件，就可以运行了\n",
    "boot.py 代码中核实模型的路径是否正确\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ca12e40-f6b0-4f90-878f-d88fd1ad0dfc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
